﻿using Corina.Commands;
using Corina.Infrastructure;
using Corina.Win.DataService;
using Corina.Win.Helpers;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Deployment.Application;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

namespace Corina.Win.ServerHub.Viewmodels
{
    public class ViewmodelBase : BaseClass
    {
        public string WebServiceAddress
        {
            get
            {
                return ConfigurationManager.AppSettings["app_webservicehost"];
            }
        }

        public Version CurrentVersion
        {
            get
            {
                return ApplicationDeployment.CurrentDeployment.CurrentVersion;
            }
        }

        protected CoreSoapClient CoreClient { get; set; }
        protected DeviceMeasureSoapClient DeviceMeasureClient { get; set; }

        #region Constructor
        public ViewmodelBase()
        {
            CoreClient = new CoreSoapClient(WebBindingConfig.HttpBinding, new EndpointAddress(WebServiceAddress + ConstUrls.URL_CORE));
            DeviceMeasureClient = new DeviceMeasureSoapClient(WebBindingConfig.HttpBinding, new EndpointAddress(WebServiceAddress + ConstUrls.URL_DEVICEMEASURE));
        }
        #endregion

        public DataContainer DataContext
        {
            get
            {
                return (DataContainer)Application.Current.MainWindow.DataContext;
            }
        }

        #region Current Account
        protected string Account
        {
            get
            {
                return DataContext.IsValid ? DataContext.CurrentAccount.Account : string.Empty;
            }
        }

        protected string Password
        {
            get
            {
                return DataContext.IsValid ? DataContext.CurrentAccount.Password : string.Empty;
            }
        }

        #endregion

        protected virtual async void WriteRunlog(params object[] paras)
        {
            await Task.Run(() =>
            {
                ErrorTraceHelper.Trace("\r\n", paras);

                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                {
                    if (ShowLog)
                    {
                        DataContext.WriteRunlog(paras);
                    }
                }));

            });
        }

        protected void ShowPrompt(string content, string title = "")
        {
            DataContext.PromptContent = content;
            DataContext.PromptHeader = title;
            DataContext.ShowPromptWindow = true;
        }

        #region Common
        private bool _isbusy = false;
        public bool IsBusy
        {
            get { return _isbusy; }
            set
            {
                _isbusy = value;
                RaisePropertyChanged(() => IsBusy);
            }
        }

        private bool ShowLog
        {
            get {
                return Convert.ToBoolean(ConfigurationManager.AppSettings["app_showlog"]);
            }
        }
        #endregion
    }
}
